/*
 * main.cpp
 *
 *  Created on: 15.07.2012
 *      Author: 116
 */
#include <stdio.h>
#include <stdlib.h>
#include <time.h>

#include "mxml.h"

#include "MandelbrotQuadTree.h"
#include "MandelbrotQuadTreeCompute.h"
#include "MandelbrotQuadTreeHelpers.h"

#include "utils.h"

MandelbrotQuadTree * exec(unsigned int pointsPerSide, unsigned int maxIter, unsigned int maxComputationDepth, unsigned int diffIterLimit, double minX,
        double maxX, double minY, double maxY)
{
    MandelbrotQuadTree * root = newRoot(minX, maxX, minY, maxY);

    double * edgeArray = malloc(sizeof(double) * pointsPerSide * 8);
    double * innerArray = malloc(sizeof(double) * pointsPerSide * pointsPerSide * 2);

    int i = 0;
    int goOn = 1;
    int maxDepth = -1;
    while (1)
    {
        if ((i % 1000) == 0)
        {
//            printf("Computation of node %i.\n", i);
//            fflush(stdout);
        }

        goOn = computeNextNode(root, edgeArray, innerArray, pointsPerSide, maxComputationDepth, maxIter, diffIterLimit);

        if (goOn > maxDepth)
        {
            maxDepth = goOn;
//            printf("Reached depth %i.\n", maxDepth);
//            fflush(stdout);
        }
        else if (goOn < 0)
        {
            break;
        }
        ++i;
    }

    return root;
}

int main(int argc, const char* argv[])
{
    printf("Start\n");

    unsigned int pointsPerSide = 100;
    unsigned int maxIter = 2048;
    unsigned int maxComputationDepth = 5;
    unsigned int diffIterLimit = 4;

    MandelbrotQuadTree * root = exec(pointsPerSide, maxIter, maxComputationDepth, diffIterLimit, -2.0, 2.0, -2.0, 2.0);

    show(root);

    printf("Took %ims\n", clock() * 1000 / CLOCKS_PER_SEC);

    writeXML(root, pointsPerSide, diffIterLimit, "./out/out.xml");

    unsigned long size = getSize(root);
    char buffer[20];
    readable_fs(size, buffer);
    printf("Size of the tree = %s\n", buffer);

    printf("End\n");
}
